
DEFINE VARIABLE vcWebResp    AS CHARACTER        NO-UNDO.
DEFINE VARIABLE lSucess      AS LOGICAL          NO-UNDO.
DEFINE VARIABLE mResponse    AS MEMPTR           NO-UNDO.
DEFINE VARIABLE mRequest       AS MEMPTR.
DEFINE VARIABLE postUrl AS CHAR.
DEFINE VARIABLE vcRequest as char format "x(100)" no-undo.
DEFINE VARIABLE postData  as char format "x(100)" no-undo.
DEFINE VARIABLE vhSocket   AS HANDLE                                NO-UNDO.
CREATE SOCKET vhSocket.

/**********Using Windows API****************/
PROCEDURE Sleep EXTERNAL "KERNEL32.DLL":
DEFINE INPUT PARAMETER intMilliseconds AS LONG.
END PROCEDURE.

vhSocket:CONNECT("-H www.saferpay.com -S 443 -ssl -nohostverify") NO-ERROR.
IF vhSocket:CONNECTED() = FALSE THEN
do:
message "Error".
MESSAGE ERROR-STATUS:GET-MESSAGE(1) VIEW-AS ALERT-BOX.

end.
else
message "Connected".
pause.
hide message no-pause.
message "GET".
postUrl = "/hosting/CreatePayInit.asp".
postData = "?spPassword=JFrY6io@qi"
          + "&ACCOUNTID=99867-94913159"
          + "&ORDERID=WEB-12345"
          + "&AMOUNT=5000"
          + "&CURRENCY=CHF"
          + "&LANGID=fr"
          + "&DESCRIPTION=Test"
          + "&SUCCESSLINK=http://www.parvis.ch/cgi-bin/cgiip.exe/WService=wsbroker1/pmt_ok.htm?xxx="
          + "&FAILLINK=http://www.parvis.ch/cgi-bin/cgiip.exe/WService=wsbroker1/pmt_nok.htm?xxx=".

 vcRequest = 'GET ' +
             postUrl +
             postData +
             " HTTP/1.0 ~r~n" +
             "Host: secure2.nmi.com" +
             "~r~nUser-Agent: " +
             "~r~nConnection: close~r~n~r~n"
             .

    SET-SIZE(mRequest)            = 0.
    SET-SIZE(mRequest)            = LENGTH(vcRequest) + 1.
    SET-BYTE-ORDER(mRequest)      = BIG-ENDIAN.
    PUT-STRING(mRequest,1)        = vcRequest .

vhSocket:WRITE(mRequest, 1, LENGTH(vcRequest)).
hide message no-pause.
message "WRITE message complete".

RUN sleep(5000).

 DO WHILE vhSocket:GET-BYTES-AVAILABLE() > 0:
           
         SET-SIZE(mResponse) = vhSocket:GET-BYTES-AVAILABLE() + 1.
         SET-BYTE-ORDER(mResponse) = BIG-ENDIAN.
         vhSocket:READ(mResponse,1,1,vhSocket:GET-BYTES-AVAILABLE()).
         vcWebResp = vcWebResp + GET-STRING(mResponse,1).
 END.
hide message no-pause.
message "READ Response complete".
pause.

hide message no-pause.
message "Response: " + vcWebResp
    VIEW-AS ALERT-BOX.
pause.

vhSocket:DISCONNECT().
DELETE OBJECT vhSocket.
message "END".
pause.

/*






DEFINE VARIABLE vcWebHost AS CHARACTER
INITIAL "www.saferpay.com" NO-UNDO.
DEFINE VARIABLE vcWebPort AS CHARACTER
INITIAL "443" NO-UNDO.
DEFINE VARIABLE vcWSAgent AS CHARACTER
INITIAL "/hosting/CreatePayInit.asp"
NO-UNDO.
/* An easy test on the WebSpeed object side (xmlagent.p ) is */
/* to check whether the WEB-CONTEXT:IS-XML is TRUE and */
/* WEB-CONTEXT:X-DOCUMENT is a valid handle. */

DEFINE VARIABLE vhWebSocket AS HANDLE NO-UNDO.

CREATE SOCKET vhWebSocket.

vhWebSocket:CONNECT('-H ' + vcWebHost + ' -S ' + vcWebPort ) NO-ERROR.

IF vhWebSocket:CONNECTED() = FALSE THEN DO:
/* error connecting to the Web Server */
END.

vhWebSocket:SET-READ-RESPONSE-PROCEDURE('getWebServerResponce').
RUN PostRequest.
WAIT-FOR READ-RESPONSE OF vhWebSocket.
vhWebSocket:DISCONNECT() NO-ERROR.
DELETE OBJECT vhWebSocket.

/* Responsible for reading server response */
PROCEDURE getWebServerResponce:

DEFINE VARIABLE vcWebResp AS CHARACTER NO-UNDO.
DEFINE VARIABLE lSuccess AS LOGICAL.
DEFINE VARIABLE mResponse AS MEMPTR.

IF SELF:CONNECTED() = FALSE THEN RETURN.

lSuccess = TRUE.
DO WHILE lSuccess AND ERROR-STATUS:GET-MESSAGE(1) EQ '' :
SET-SIZE(mResponse) = 1.
SET-BYTE-ORDER(mResponse) = BIG-ENDIAN.
SELF:READ(mResponse,1,1,1).
vcWebResp = vcWebResp + GET-STRING(mResponse,1).
IF SELF:CONNECTED() = FALSE THEN LEAVE.
END.

/* just to see what is coming back */
OUTPUT TO "c:\thesis\temp\xmlsend.log" .

put unformatted "BEGIN: " STRING ( TIME , "hh:mm:ss" ) SKIP.
put unformatted vcWebResp SKIP.
put unformatted "END: " STRING ( TIME , "hh:mm:ss" ) SKIP.

output close.

END PROCEDURE.

/* make a request and post the xml document */
PROCEDURE PostRequest:
DEF VAR viXMLLength AS INTEGER.
DEF VAR vcRequest AS CHARACTER.
DEF VAR viRequest AS INTEGER.
DEF VAR viMsg AS INTEGER.
DEF VAR mRequest AS MEMPTR.
DEF VAR lSuccess AS LOGICAL.

/* just to be very simple, the XML document is stored in this var */
DEFINE VARIABLE vcXMLText AS CHARACTER NO-UNDO.
vcXMLText = "?spPassword=JFrY6io@qi"
          + "&ACCOUNTID=99867-94913159"
          + "&ORDERID=WEB-12345"
          + "&AMOUNT=5000"
          + "&CURRENCY=CHF"
          + "&LANGID=fr"
          + "&DESCRIPTION=Test"
          + "&SUCCESSLINK=http://www.parvis.ch/cgi-bin/cgiip.exe/WService=wsbroker1/pmt_ok.htm?xxx="
          + "&FAILLINK=http://www.parvis.ch/cgi-bin/cgiip.exe/WService=wsbroker1/pmt_nok.htm?xxx=".


/* - <article lang="de" questionid="1" requested="bnslt">  */
/*   <groessenr>FN219-120000-5</groessenr>                 */
/*   <groesseid>24607643</groesseid>                       */
/*   <menge>1</menge>                                      */
/*   </article>                                            */
/*   </client>                                             */
/*   </erprequest>                                         */

ASSIGN
viXMLLength = LENGTH ( vcXMLText )
vcRequest =
'POST ' + vcWSAgent
/* + ' HTTP/1.0~n'                                    */
/* + 'Content-Type: text/html~n' /* post XML DOC */   */
/* + 'Host: ' + vcWebHost + '~n'                      */
/* + 'Content-Length: ' + STRING(viXMLLength) + '~n'  */
/* + 'Accept: text/html~n'                            */
/* + '~n'                                             */
viRequest = LENGTH ( vcRequest )
viMsg = viRequest + viXMLLength + 1
.

MESSAGE  vcRequest + vcXMLText VIEW-AS ALERT-BOX.

SET-SIZE(mRequest) = 0.
SET-SIZE(mRequest) = viMsg.
SET-BYTE-ORDER(mRequest) = BIG-ENDIAN.

PUT-STRING(mRequest,1) = vcRequest + vcXMLText.

vhWebSocket:WRITE(mRequest,1,viMsg) NO-ERROR.

IF ERROR-STATUS:ERROR THEN DO:
/* error writing to socket */
END.
END PROCEDURE.
  */
